<template>
  <div class="appContainer">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="电话号码">
        <el-input v-model="formInline.tel" placeholder="输入司机电话"></el-input>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-input v-model="formInline.numberPlate" placeholder="输入车牌号"></el-input>
      </el-form-item>
      <el-form-item label="城市">
        <el-input v-model="formInline.targetCity" placeholder="输入目的地城市"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="formInline.type" clearable placeholder="请选择车辆类型">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doSearch">查询</el-button>
      </el-form-item>

    </el-form>

    <el-dialog
        title="修改信息"
        :visible.sync="dialogVisible1"
        width="30%"
        :before-close="dialogClose">
      <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
        <el-form-item label="姓名">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="formLabelAlign.tel"></el-input>
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input v-model="formLabelAlign.numberPlate"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="formLabelAlign.type" clearable placeholder="请选择车辆类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="EditDriver">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
        title="新增司机"
        :visible.sync="dialogVisible2"
        width="30%"
        :before-close="dialogClose">
      <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
        <el-form-item label="姓名">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="formLabelAlign.tel"></el-input>
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input v-model="formLabelAlign.numberPlate"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="formLabelAlign.type" clearable placeholder="请选择车辆类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="addDriver">确 定</el-button>
      </span>
    </el-dialog>

    <el-button type="danger" :disabled="disabledAdd" @click="deleteSelected" plain size="small" style="margin-bottom: 10px">删除此页所选</el-button>
    <el-button type="success" :disabled="disabledAdd" @click="newDriver" plain size="small" style="margin-bottom: 10px">新增</el-button>

    <el-table
        :data="tableData"
        border
        stripe
        width="100%"
        @select="selected"
        @selection-change="handleSelectionChange">

      <el-table-column
          fixed
          label="全选"
          align="center"
          type="selection"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          prop="id"
          label="id"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          prop="name"
          label="姓名"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          prop="tel"
          label="电话"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          prop="numberPlate"
          label="车牌号"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          prop="type"
          label="车型"
          width="auto">

        <template slot-scope="scope">
          <span v-if="scope.row.type=='small'">小型车</span>
          <span v-if="scope.row.type=='medium'">中型车</span>
          <span v-if="scope.row.type=='big'">大型车</span>
          <span v-if="scope.row.type=='plus'">集装箱</span>
        </template>

      </el-table-column>
      <el-table-column
          align="center"
          prop="lal"
          label="当前位置"
          width="180">

        <template slot-scope="scope">
          <el-popover
              width="400"
              @show="delayMap(scope.row.lal)"
              placement="top-start"
              trigger="hover"
              content="弹窗里插地图">

            <span slot="reference">{{ scope.row.lal }}</span>
            <baidu-map
                class="map"
                :center="curLal"
                :zoom=5
                :scroll-wheel-zoom=true
                >
              <bm-marker
                  :position="curLal"
                  animation="BMAP_ANIMATION_DROP">
              </bm-marker>
              <bm-driving
                  :start="curLal"
                  :end="scope.row.targetCity"
                  >
              </bm-driving>


            </baidu-map>

          </el-popover>
        </template>

      </el-table-column>
      <el-table-column
          align="center"
          prop="targetCity"
          label="目的地城市"
          width="auto">
      </el-table-column>
      <el-table-column
          align="center"
          label="订单状态"
          width="auto">
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">空闲</span>
          <span v-if="scope.row.status==1">忙</span>
        </template>
      </el-table-column>
      <el-table-column
          v-if="false"
          align="center"
          prop="uid"
          label="uid"
          width="auto">
      </el-table-column>

      <el-table-column
          align="center"
          label="操作"
          width="150">
        <template slot-scope="scope">
          <el-button :disabled="disabledAdd" @click="doEdit(scope.row)" type="primary" plain size="small">修改</el-button>
          <el-button :disabled="disabledAdd" @click="doDelete(scope.row)" type="danger" plain size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        style="margin-left: 280px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.currentPage"
        :page-sizes="[10,3]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize">

    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "DriverView",
  methods: {
    delayMap(lal){
      let _this = this;

      this.showModal = true

      let split = lal.split(",");

      setTimeout(function () {
        //设置经纬度
        _this.curLal={lng:split[0],lat:split[1]};

      } ,1000 * 0.1); //时间设置为0也是可以

    },

    //搜索
    doSearch(){
      this.findByPage();
    },

    //根据车型
    selectType(command){
      this.formInline.type=command;
    },

    //返回选中数组
    selected(row){
      this.allToDelete.push(row[row.length-1].id);
      console.log(this.allToDelete);
    },

    //修改与删除
    doEdit(row) {
      this.formLabelAlign = row;
      this.dialogVisible1 = true;
    },

    EditDriver(){
      const loading = this.$loading({
        lock: true,
        text: '正在提交...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.dialogVisible = false;
      this.axios.put("http://localhost:8080/driver",this.formLabelAlign).then(
          res => {
            if(res.data.code==200){
              loading.close();
              this.$message.success("提交成功");
              location.reload();
            }else {
              loading.close();
              this.$message.error("提交失败");
            }
          }
      ).catch(
          err => {
            loading.close();
            this.$message.error("提交失败");
          }
      )
    },

    doDelete(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete("http://localhost:8080/driver/"+row.id).then(
            res=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              location.reload();
            }
        )
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    //删除选中
    deleteSelected() {
      this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete("http://localhost:8080/driver/"+this.allToDelete).then(
            res=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              location.reload();
            }
        )
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    //误点
    dialogClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },

    //新增司机窗口
    newDriver(){
      this.dialogVisible2 = true
      this.formLabelAlign = {
        tel:'',
        numberPlate:'',
        type:'',
        id:'',
        lal:'',
        targetCity:'',
        status:'0',
        uid:'',
        name:''
      }
    },

    //新增司机方法
    addDriver(){
      const loading = this.$loading({
        lock: true,
        text: '正在提交...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.axios.post("http://localhost:8080/driver",this.formLabelAlign).then(
          res => {
            if(res.data.code==200){
              loading.close();
              this.dialogVisible2 = false;
              this.$message.success("提交成功");
              location.reload();
            }else {
              loading.close();
              this.$message.error("提交失败");
              console.log(res)
            }
          }
      ).catch(
          err => {
            loading.close();
            this.$message.error("提交失败");
          }
      )
    },

    //全选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    findByPage() {
      const loading = this.$loading({
        lock: true,
        text: '正在查询..',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.axios.get("http://localhost:8080/driver/list?"+this.tansParams(this.formInline)).then(
          res => {
            if(res.data.code==200){
              loading.close();
              this.tableData = res.data.data.list;
              this.totalSize = res.data.data.total;
              this.$message.success("查询成功");
              console.log(res.data.data);
            }else {
              loading.close();
              this.$message.error("查询失败");
            }
          }
      ).catch(
          err => {
            loading.close();
            this.$message.error("查询失败");
          }
      )
    },
    //分页
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.findByPage();
    },
    handleCurrentChange(val) {
      this.formInline.currentPage = val;
      this.findByPage();
    },

    tansParams(params) {
      let result = ''
      for (const propName of Object.keys(params)) {
        const value = params[propName];
        var part = encodeURIComponent(propName) + "=";
        if (value !== null && typeof (value) !== "undefined") {
          if (typeof value === 'object') {
            for (const key of Object.keys(value)) {
              if (value[key] !== null && typeof (value[key]) !== 'undefined') {
                let params = propName + '[' + key + ']';
                var subPart = encodeURIComponent(params) + "=";
                result += subPart + encodeURIComponent(value[key]) + "&";
              }
            }
          } else {
            result += part + encodeURIComponent(value) + "&";
          }
        }
      }
      return result
    }
  },

  created() {
    this.findByPage();
  },
  data() {
    return {
      displayMarker:false,
      curLal:'',
      totalSize: 10,
      disabledAdd: false, //是否可以编辑
      allToDelete:[], //复选框数组
      formLabelAlign: {
        tel:'',
        numberPlate:'',
        type:'',
        id:'',
        lal:'',
        targetCity:'',
        status:'',
        uid:'',
        name:''
      },
      labelPosition: 'right',
      dialogVisible1: false, //是否显示
      dialogVisible2: false, //是否显示
      formInline: { //搜索表单数据
        pageSize: 10,
        currentPage: 1,
        tel: '',
        numberPlate: '',
        type: '',
        targetCity: ''
      },
      tableData: [{
        id: '154135165',
        name: '王小虎',
        tel: '18164565665',
        numberPlate: '川A12345',
        type:'big',
        lal:'104.065735,30.659462',
        targetCity:'成都',
        status:'0'
      }],
      options: [{
        value: 'small',
        label: '小型车'
      }, {
        value: 'medium',
        label: '中型车'
      }, {
        value: 'big',
        label: '大型车'
      }, {
        value: 'plus',
        label: '集装箱'
      }],
    }
  }
}
</script>

<style scoped>
  .appContainer{
    text-align: left;
  }
  .map {
    width: 100%;
    height: 400px;
  }
</style>